<template>
  <div class="mod-config">
    <div v-if="member">
      <el-form :inline="true" :model="dataForm" @keyup.enter.native="search()" ref="dataForm">
        <el-form-item>
          <el-input v-model="dataForm.channelName" placeholder="渠道分类"></el-input>
        </el-form-item>
        <el-form-item label="渠道属性：">
          <el-select v-model="dataForm.channelType" placeholder="请选择">
            <el-option
              v-for="item in useroption"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="search()">查询</el-button>
          <el-button type="primary" @click="addChannel()">添加渠道</el-button>
        </el-form-item>
      </el-form>
      <el-table
        :data="dataList"
        border
        v-loading="dataListLoading"
        style="width: 100%;"
      >
        <el-table-column prop="channelName" header-align="center" align="center" label="渠道分类"></el-table-column>
        <el-table-column header-align="center" align="center" label="渠道属性">
          <template slot-scope="scope">
            {{ scope.row.channelType === 1?'公众号':'' }}
            {{ scope.row.channelType === 2?'微博':'' }}
            {{ scope.row.channelType === 3?'抖音':'' }}
            {{ scope.row.channelType === 4?'其他平台':'' }}
          </template>
        </el-table-column>
        <el-table-column prop="adNum" header-align="center" align="center" label="投放数量"></el-table-column>
        <el-table-column prop="channelRemark" header-align="center" align="center" label="备注"></el-table-column>
        <el-table-column prop="gmtCreated" header-align="center" align="center" label="创建时间"></el-table-column>
        <el-table-column prop="gmtUpdate" header-align="center" align="center" label="更新时间"></el-table-column>
        <el-table-column fixed="right" header-align="center" align="center" label="操作" width="150px">
          <template slot-scope="scope">
            <el-button type="text" size="small" @click="addChannel(scope.row)">编辑</el-button>
            <el-button type="text" size="small" @click="delChannel(scope.row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        @size-change="sizeChangeHandle"
        @current-change="currentChangeHandle"
        :current-page="pageIndex"
        :page-sizes="[10, 20, 50, 100]"
        :page-size="pageSize"
        :total="totalPage"
        layout="total, sizes, prev, pager, next, jumper"
      ></el-pagination>

      <!--添加渠道s-->
      <el-dialog
        :close-on-click-modal="false"
        :visible.sync="visible1"
        :title="isAdd?'添加渠道':'修改渠道'"
      >
        <el-form
          :model="dataForm1"
          ref="dataForm1"
          label-width="150px"
          :rules="rules"
        >
          <el-form-item label="渠道名称:" prop="channelName">
              <el-input v-model="dataForm1.channelName"></el-input>
          </el-form-item>
          <el-form-item label="渠道分类:" prop="channelType">
            <el-select v-model="dataForm1.channelType" placeholder="请选择">
              <el-option
                v-for="item in useroption"
                :key="item.value"
                :label="item.label"
                :value="item.value">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="备注:" prop="channelRemark">
            <el-input v-model="dataForm1.channelRemark" placeholder="选填"></el-input>
          </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button @click="visible1 = false">取消</el-button>
          <el-button type="primary" @click="channelForm">确定</el-button>
        </span>
      </el-dialog>
      <!--添加渠道e-->

    </div>
  </div>
</template>

<script>
  export default {
    data() {
      return {
        member: true,
        isAdd: true,
        visibleIntegralList: false,
        dataForm: {
          channelType: null,
          channelName: ''
        },
        dataForm1: {
          channelType: '',
          channelName: '',
          channelRemark: ''
        },
        rules: {
          channelName: [
            { required: true, message: '请输入渠道名称', trigger: 'blur' }
          ],
          channelType: [
            { required: true, message: '请选择渠道分类', trigger: 'blur' }
          ]
        },
        zanConfig: 0,
        visible1: false,
        dataList: [],
        dataListLoading: false,
        pageIndex: 1,
        pageSize: 10,
        totalPage: 0,
        useroption: [
          {
            value: null,
            label: '全部'
          },
          {
            value: 1,
            label: '公众号'
          },
          {
            value: 2,
            label: '微博'
          },
          {
            value: 3,
            label: '抖音'
          },
          {
            value: 4,
            label: '其他平台'
          }
        ]
      }
    },
    activated() {
      this.getDataList()
    },
    methods: {
      search() {
        this.pageIndex = 1
        this.getDataList()
      },
      // 获取数据列表
      getDataList() {
        this.dataListLoading = true
        this.dataForm.page = this.pageIndex
        this.dataForm.limit = this.pageSize
        this.$http({
          url: this.$http.adornUrl('/market/marketchannel/list'),
          method: 'get',
          params: this.$http.adornParams(this.dataForm)
        }).then(({ data }) => {
          if (data && data.code === 0) {
            this.$refs['dataForm'].resetFields()
            this.dataList = data.page.list
            this.totalPage = data.page.totalCount
          } else {
            this.dataList = []
            this.totalPage = 0
          }
          this.dataListLoading = false
        })
      },
      addChannel(channelInfo) {
        if(channelInfo) {
          this.isAdd = false
          this.dataForm1 = channelInfo
        } else {
          this.isAdd = true
          this.dataForm1 = {
            channelType: '',
            channelName: '',
            channelRemark: ''
          }
        }
        this.visible1 = true
      },
      delChannel(id) {
        this.$confirm('删除渠道后，将删除该渠道下所有投放，是否确认删除？', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.$http({
            url: this.$http.adornUrl('/market/marketchannel/delete'),
            method: 'post',
            data: [id]
          }).then(({data}) => {
            if(data.code === 0) {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.getDataList()
                }
              });
            }
          })
        }).catch(() => {

        })
      },
      channelForm() {
        let url = ''
        if(this.dataForm1.id) {
          url = '/market/marketchannel/update'
        } else {
          url = '/market/marketchannel/save'
        }
        this.$refs['dataForm1'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(url),
              method: 'post',
              data: this.$http.adornData(this.dataForm1)
            }).then(({data}) => {
              if(data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible1 = false
                    this.getDataList()
                  }
                });
              }
            })
          }
        })
      },
      // 每页数
      sizeChangeHandle(val) {
        this.pageSize = val
        this.pageIndex = 1
        this.getDataList()
      },
      // 当前页
      currentChangeHandle(val) {
        this.pageIndex = val
        this.getDataList()
      }
    }
  }
</script>
<style>
  .margin_10{
    margin-bottom: 10px;
  }
  .text_r{
    text-align: right;
    padding-right: 15px;
  }
  .inineBlock{
    display: inline-block;
  }
  img{
    width: 100%;
    height: auto;
  }
</style>
